# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  def digest_enc(string)
		begin 
		  Digest::MD5.hexdigest(Digest::SHA1.hexdigest(string))
		rescue 
		  require "digest/md5"
		  require "digest/sha1"
		  Digest::MD5.hexdigest(Digest::SHA1.hexdigest(string))
		# else
		# ensure
		end
	end

	def login?
		begin 
			session[:uinfo][:login_state] == true
		rescue 
			false
		# else
		# ensure
		end
	end

	def authentication_login
		if(login?) then
		else
			session[:to_url]=request.env["REQUEST_URI"]
			redirect_to :controller=>:session,:action=>:default
		end
	end

  	def useable?
		puts "helper is userable"
	end

	  def rich_text_editor(object, method, options = {})
		fckeditor_textarea(object, method, options.merge({:toolbarSet => 'Simple', :height => '300px'}))
	end

	def date_time
		Time.now.strftime("%Y-%m-%d %H:%M:%S")
	end

	def to_date_time
			self.strftime("%Y-%m-%d %H:%M:%S")
	end
#--------ImageHelper-----------
        def getFileName(filename)
		if !filename.nil?
			Time.now.strftime("%Y_%m_%d_%H_%M_%S") + '_' + filename
		end
	end

	def uploadFile(file)
		#file可以通过params[:object][:file]得到
		if !file.original_filename.empty?
			@filename=getFileName(file.original_filename)
			file_path="#{RAILS_ROOT}/public/images/#{@filename}"
			#type=file.original_filename.split('.').last
			File.open(file_path, "wb") do |f|
				f.write(file.read)
			end
			return file_path,file.content_type.chomp,@filename
		end
	end  

#--------DocumentHelper-----------
	def show_children(children)
		content=''
		if(children.size>0) then
			children.each do |child|
				url = link_to(h(child.title||"空标题"),document_path(child.id))
				margin_left = child.depth * 20
				content << %(
					<div style="margin-left:#{margin_left}px">
						#{url} by #{h(child.member.nickname)} &middot;#{child.create_date.strftime("%Y-%m-%d %H:%M:%S")}
					</div>
				)
			end
		end
		content
	end
end
